#include "selectthread.h"

#include <QDebug>

SelectThread::SelectThread(QObject *parent) : QObject(parent)
{

}

SelectThread::~SelectThread()
{
    qInfo() << "[SELECT] Thread Release Resources";
}

void SelectThread::startThread_slot()
{
    qInfo() << "[SELECT] Thread Start";

    QTimer *m_timer = new QTimer;
    connect(m_timer, &QTimer::timeout, this, &SelectThread::onSelect_slot);
    m_timer->start(95);
}

void SelectThread::exitThread_slot()
{
    qInfo() << "[SELECT] Thread Exit";
}

void SelectThread::onSelect_slot()
{
    qDebug() << "[SELECT] On Select Slot";

    int ret = 0;
    QQueue<DISEASE_INFO> m_diseInfoQueue;

    // Select DR Data
    ret = database.drDataSelect(currentID, m_diseInfoQueue);
    if (ret == -1)
    {
        currentID = 0;
        qWarning() << "[SELECT] DR Data Select Failed DB Not Exit";
        return;
    }
    else if (ret != 0)
    {
        qWarning() << "[SELECT] DR Data Select Failed" << ret;
        return;
    }

    int lastID = 0;
    int size = m_diseInfoQueue.size();
    if (size != 0)
    {
        lastID = m_diseInfoQueue.last().sql_id.toInt();
    }

    currentID += size;
    emit sendCurrentID_signal(currentID);
    qInfo() << "[SELECT] DR Data Select ID" << currentID << lastID;
}
